package com.delay.queue.json;

import com.alibaba.fastjson.JSONObject;
import com.google.gson.GsonBuilder;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

public class JsonUtil {

    static GsonBuilder gb = new GsonBuilder();

    static {
        gb.disableHtmlEscaping();
    }

    public static String pojoToJsonStr(Object obj) {
//        return new Gson().toJson(obj);
        return gb.create().toJson(obj);
    }

    public static <T> T jsonStrToPojo(String json, Class<T> clazz) {
        return JSONObject.parseObject(json, clazz);
    }

    public static void main(String[] args) {
        People people = new People("Tom", 20);
        String json = pojoToJsonStr(people);
        System.out.println(pojoToJsonStr(people));

        People people1 = jsonStrToPojo(json, People.class);
        System.out.println(people1);
    }


}

@Data
@NoArgsConstructor
@AllArgsConstructor
class People {
    private String name;
    private Integer age;
}
